package gdt.mvc.converters;

import gdt.dao.MemberDAO;
import gdt.entity.Member;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

import org.springframework.web.jsf.FacesContextUtils;



public class MemberConverter implements Converter {

	@Override
	public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException {
		if(value!=null){
			MemberDAO memberDao = (MemberDAO)FacesContextUtils.getWebApplicationContext(context).getBean("memberDao");
			return memberDao.findById(Long.parseLong(value));
		}
		else
			return null;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component, Object value) throws ConverterException {
		if(value == null)
			return null;
		else
			return Long.toString(((Member)value).getId());
	}

}